package com.bianguo.android.functiondemo.view.widget;

import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.ViewGroup;

public class SlideItemTouchHelperCallback extends ItemTouchHelper.Callback {

    private OnItemTouchListener onItemTouchListener;

    public SlideItemTouchHelperCallback(OnItemTouchListener onItemTouchListener) {
        this.onItemTouchListener = onItemTouchListener;
    }

    /**
     * 需要处理的方向 ， 如上下移动 ， 左右滑动等
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        //我要监听的拖拽方向是哪个方向
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //我要监听的滑动方向是哪个方向
        int swipeFlags = ItemTouchHelper.LEFT/* | ItemTouchHelper.RIGHT*/;
        int flags = makeMovementFlags(dragFlags, swipeFlags);
        return flags;
    }

    /**
     * 开启长按拖拽
     * @return
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return true;
    }

    /**
     * 移动时会回调
     * @param recyclerView
     * @param viewHolder
     * @param viewHolder1
     * @return
     */
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        // 在拖拽过程中不断地调用adapter.notifyItemMoved(from,to);
        if (viewHolder.getItemViewType() != viewHolder1.getItemViewType()) {
            return false;
        }
        //在拖拽的过程中不断调用adapter.notifyItemMoved(from,to);
//        boolean result = moveListener.onItemMove(viewHolder.getAdapterPosition(), viewHolder1.getAdapterPosition());
        onItemTouchListener.onItemSwap(viewHolder.getAdapterPosition(), viewHolder1.getAdapterPosition());
        return true;
    }

    /**
     * 滑动时会回调
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        //监听侧滑，1.删除数据 2.调用adapter.notifyItemRemove(position);
//        moveListener.onItemRemove(viewHolder.getAdapterPosition());
        onItemTouchListener.removeItem(viewHolder.getAdapterPosition());
    }

    /**
     * 选中Item状态
     * @param viewHolder
     * @param actionState Item状态
     */
    @Override
    public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
        if (actionState == ItemTouchHelper.ACTION_STATE_IDLE) {
            return;
        }
//        viewHolder.itemView.setBackgroundColor(Color.GRAY);
        super.onSelectedChanged(viewHolder, actionState);
    }

    /**
     * ItemView复位回调
     * @param recyclerView
     * @param viewHolder
     */
    @Override
    public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        // Item会复用 ， 所有操作ItemView的状态之后一定要复原
        viewHolder.itemView.setBackgroundColor(Color.WHITE);
        viewHolder.itemView.scrollTo(0, 0);
        super.clearView(recyclerView, viewHolder);
    }

    /**
     * 操作Item的时候会不断调用绘制Item ， 我们可以在这里做很多事情。
     * @param c
     * @param recyclerView
     * @param viewHolder
     * @param dX
     * @param dY
     * @param actionState
     * @param isCurrentlyActive
     */
    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    /**
     * 获取删除方块的宽度
     */
    private int getSlideLimitation(RecyclerView.ViewHolder viewHolder){
        ViewGroup viewGroup = (ViewGroup) viewHolder.itemView;
        View childView = viewGroup.getChildAt(1);
        return childView.getLayoutParams().width;
    }

    public interface OnItemTouchListener {
        void onItemSwap(int position, int tagPosition);

        void removeItem(int position);
    }
}
